package ml.evaluation;

import java.util.List;

import ml.LatchedRunnable;
import ml.UpAndCountDownLatch;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.Instances;

public abstract class EvaluationRunnable extends LatchedRunnable {

	protected final Instances trainingData;
	protected final Instances testData;
	protected final List<EvaluationResult> results;

	public EvaluationRunnable(final UpAndCountDownLatch latch,
			final Instances trainingData, final Instances testData,
			final List<EvaluationResult> results) {
		super(latch);
		this.trainingData = trainingData;
		this.testData = testData;
		this.results = results;
	}

	@Override
	public void latchedExec() throws Exception {
		results.add(evaluate());
	}

	abstract EvaluationResult evaluate() throws Exception;

	protected EvaluationResult evaluateOnTestData(final Classifier classifier)
			throws Exception {
		classifier.buildClassifier(trainingData);
		final Evaluation eval = new Evaluation(trainingData);
		eval.evaluateModel(classifier, testData);
		return new EvaluationResult(classifier.getOptions(), eval);
	}
}
